#!/Library/Frameworks/Python.framework/Versions/2.6/bin/python
import cgi
import cgitb
cgitb.enable()
import os
import urllib2
import sys
import processYPGForYCR
import time

pplog = open("./pplog", "a")
starttime = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
requesturi = os.environ.get("REQUEST_URI")
pplog.write("\nrequest:" + requesturi + " time:" + starttime + "\n")

try:
	#params = os.environ.get("REQUEST_URI").split("pyproxy",1)
	# Create instance of FieldStorage
	qform = cgi.FieldStorage()
	# Get data from fields
	qycr = qform.getvalue('ycr')
	qparentA = qform.getvalue('pa')
	qparentB = qform.getvalue('pb')
	qstart = qform.getvalue('ps')
	qcount = qform.getvalue('pc')
	qstrainsPerPlate = qform.getvalue('spp')
	qstrainsPerTetrad = qform.getvalue('spt')
	qplate = qform.getvalue('ipn')
	contact = qform.getvalue('ct')
	pplog.write("start main(...) for %s %s\n" %(qycr, qcount))
	processYPGForYCR.main(qycr, qparentA, qparentB, qcount, qstart, qstrainsPerPlate, qstrainsPerTetrad, qplate, contact, pplog)
	print "Content-type: text/plain; charset=iso-8859-1\n\n"
	print "{status:'200'}"
except:
	e = sys.exc_info()[1]
	endtime = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
	pplog.write("\nException/error\n" + str(e) + "\n")
	print "{status:'error'}"	

pplog.close()
